<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="closeFn">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template v-slot:footer>
      <el-row type="flex" justify="center">

        <el-button type="primary" @click="comfirmFn">确定</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { assignRoles } from '@/api/employees'
import { getRoleList } from '@/api/setting'
import { getUserInfoById } from '@/api/user'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      checkList: [],
      roleList: []
    }
  },
  async created() {
    const res = await getRoleList({
      page: 1,
      pagesize: 100
    })
    this.roleList = res.rows
    console.log('roleList', this.roleList)
  },
  methods: {
    closeFn() {
      this.$emit('update:showDialog', false)
    },
    async getroleList() {
      const res = await getUserInfoById(this.userId)
      console.log(res)
      this.checkList = res.roleIds
    },
    // 编辑角色
    async  editRole(id) {
      this.userId = id // props传值 是异步的
      await this.$refs.assignRole.getUserDetailById(id) // 父组件调用子组件方法
      this.showRoleDialog = true
    },
    async comfirmFn() {
      await assignRoles({
        id: this.userId,
        roleList: this.checkList
      })
      console.log('checkList', this.checkList)
      this.$emit('update:showDialog', false)
    }

  }
}
</script>

<style>

</style>
